function setFormInput(dataMap,formId){
	var getData = function(dataMap,obj){
		var name = obj.attr("name").split(".");
		name = name[name.length-1];
		var val = dataMap[name];
		return val;
	};
	var textInputs = $("#" + formId).find("input:not(:button)");
	for(var i=0;i < textInputs.length ; i++){
		var obj = $(textInputs[i]);
		obj.val(getData(dataMap,obj));
	}
	
	var selects = $("#" + formId).find("select");
	for(var i=0;i < selects.length ; i++){
		var obj = $(selects[i]);
		obj.val(getData(dataMap,obj));
	}
}
function clickTreeSelect(){
	var treeObj = $.fn.zTree.getZTreeObj("menuTree");
	var node = treeObj.getSelectedNodes()[0];
	treeOnClick(null, null, node);
}
function treeOnClick(event, treeId, treeNode) {
	cancleFormBtn();
    if(treeNode.type == 'menu'){
    	loadMenu(treeNode);
    }else{
    	loadBtn(treeNode);
    }
}
var setting = {
	/*check: {
		enable: true,
		chkboxType:{ "Y" : "s", "N" : "s" }
	},*/
	data: {
		simpleData: {
			enable: true,
			idKey: "id",
            pIdKey: "pid",
            rootPId: ""
		},
		key:{name:"name"}
	},
	callback:{
		//单击事件
		onClick:treeOnClick
	}
}

function loadMenu(treeNode){
	$("#menu_from_div").show();
	$("#button_form_div").hide();
	var param = {};
	param.menu_id = treeNode.id;
	$.ajax({
		url : "/admin/menu/loadMenuForID.do",
		data:param,
		type:"post",
		dataType:"json",
		success : function(data) {
			setFormInput(data,"menu_form")
		}
	});
}
function loadBtn(treeNode){
	$("#menu_from_div").hide();
	$("#button_form_div").show();
	var param = {};
	param.buttonId = treeNode.id;
	$.ajax({
		url : "/admin/button/loadButtonForID.do",
		data:param,
		type:"post",
		dataType:"json",
		success : function(data) {
			setFormInput(data,"button_form")
		}
	});
}

var pageLoad = function(){
	menuTreeInit();
}
function menuTreeInit(){
	$.ajax({
		url : "/admin/menu/loadAllMenuBtn.do",
		type:"post",
		dataType:"json",
		success : function(data) {
			$.fn.zTree.init($("#menuTree"), setting, data);
		}
	});
}

function btnAddChildrenMenu(btnObj){
	var url = "/admin/menu/addMenu.do";
	$(btnObj).parents("form").attr("action",url);
	
	$("#menu_from_div").show();
	$("#button_form_div").hide();
	
	var treeObj = $.fn.zTree.getZTreeObj("menuTree");
	var nodes = treeObj.getSelectedNodes()[0];
	var menu_id = nodes.id;
	$('#menu_form')[0].reset()
	$("input[name='systemMenu.parent_id']").val(menu_id);
	
	$("form").find("button").hide();
	$("form").parent().find("button[name='operation']").show();
}

function btnEditMenu(btnObj){
	var url = "/admin/menu/updateMenu.do";
	$(btnObj).parents("form").attr("action",url);
	
	$("#menu_from_div").show();
	$("#button_form_div").hide();
	
	$("form").find("button").hide();
	$("form").parent().find("button[name='operation']").show();
}

function btnSubmit(btnObj){
	var form = $(btnObj).parents("form");
	$.ajax( {
		url : form.attr("action"),
		type:"post",
		dataType:"json",
		data:form.serialize(),
		success : function(data) {
			var obj = data.obj;
			$.gritter.add({
				title: '提示',
				text: data.msg,
				class_name: 'gritter-success'
			});
//			clickTreeSelect();
			$('#menu_form')[0].reset();
			cancleFormBtn();
			menuTreeInit();
		}
	});
}

function btnAddBtn(btnObj){
	var url = "/admin/button/addButton.do";
	$("#button_form").attr("action",url);
	
	$("#menu_from_div").hide();
	$("#button_form_div").show();
	
	var treeObj = $.fn.zTree.getZTreeObj("menuTree");
	var nodes = treeObj.getSelectedNodes()[0];
	var menu_id = nodes.id;
	$('#button_form')[0].reset()
	$("input[name='systemButton.menu_id']").val(menu_id);
	
	$("form").find("button").hide();
	$("form").parent().find("button[name='operation']").show();
}
function btnEditBtn(btnObj){
	
	var url = "/admin/button/updateButton.do";
	$(btnObj).parents("form").attr("action",url);
	
	$("#menu_from_div").hide();
	$("#button_form_div").show();
	
	$("form").find("button").hide();
	$("form").parent().find("button[name='operation']").show();
}

function cancleFormBtn(){
	$("form").find("button").hide();
	$("form").parent().find("button[name!='operation']").show();
}

function btnCancelOnClick(){
	clickTreeSelect();
	cancleFormBtn();
}

function delteMenu(){
	var treeObj = $.fn.zTree.getZTreeObj("menuTree");
	var nodes = treeObj.getSelectedNodes()[0];
	var menu_id = nodes.id;
	var param = {};
	param.menu_id = nodes.id;
	var callBack = function(){
		$.ajax( {
			url : "/admin/menu/deleteMenu.do",
			type:"post",
			dataType:"json",
			data:param,
			success : function(data) {
				$.gritter.add({
					title: '提示',
					text: data.msg,
					class_name: 'gritter-success'
				});
				$('#menu_form')[0].reset();
				cancleFormBtn();
				menuTreeInit();
			}
		});
	};
	bootbox.confirm("是否删除菜单所包含的子菜单、按钮，删除后无法回退?", function(result) {
		if(result) {
			callBack();
		}
	});
}

function delteButton(){
	var treeObj = $.fn.zTree.getZTreeObj("menuTree");
	var nodes = treeObj.getSelectedNodes()[0];
	var menu_id = nodes.id;
	var param = {};
	param.btnId = nodes.id;
	var callBack = function(){
		$.ajax( {
			url : "/admin/button/deleteButton.do",
			type:"post",
			dataType:"json",
			data:param,
			success : function(data) {
				$.gritter.add({
					title: '提示',
					text: data.msg,
					class_name: 'gritter-success'
				});
				$('#menu_form')[0].reset();
				cancleFormBtn();
				menuTreeInit();
			}
		});
	};
	bootbox.confirm("是否删除按钮，删除后无法回退?", function(result) {
		if(result) {
			callBack();
		}
	});
}